﻿/*************************************************************************/
/*******************Codinng by C@T************************** *************/
/*************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn;
    string _new="";
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString);        
    }

    protected bool CheckExistStaff(string staffID)
    {
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "SELECT StaffID FROM Staff WHERE StaffID=" + staffID;
        comm.CommandType = System.Data.CommandType.Text;
        comm.Connection = conn;
        SqlDataReader read = comm.ExecuteReader();
        if (read.HasRows != null)
        {
            return false;
        }        
        return true;
    }

    protected void gvwStaffProfile_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        Regex rx;
        if (e.NewValues["StaffID"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["StaffID"], "Staff ID can't be null.");
        }
        else
        {
            if (e.NewValues["StaffID"].ToString().Length < 11)
            {
                AddError(e.Errors, gvwStaffProfile.Columns["StaffID"], "Staff ID must be at least 11 characters long. Ex: T010113-T01");
            }
            else
            {
                if (e.NewValues["StaffID"].ToString().Length > 13)
                {
                    AddError(e.Errors, gvwStaffProfile.Columns["StaffID"], "Staff ID must be at more than 13 characters long. Ex: T010113-T01");
                }
                else
                {
                    string staffid = "^T[0-9]{6}-T[0-9]{2,3}$";
                    rx = new Regex(staffid, RegexOptions.IgnoreCase);
                    if (e.NewValues["StaffID"] != null && !rx.IsMatch(e.NewValues["StaffID"].ToString()))
                    {
                        AddError(e.Errors, gvwStaffProfile.Columns["StaffID"], "Staff ID invalid. (Ex: T010113-T01)");
                    }
                    else
                    {
                        _new = e.NewValues["StaffID"].ToString();
                    }
                }
                //else
                //{
                //    if (!CheckExistStaff(e.NewValues["StaffID"].ToString()))
                //    {
                //        AddError(e.Errors, gvwStaffProfile.Columns["StaffID"], "Staff ID is to exist. Please use with orther Staff ID");
                //    }                    
                //}
            }
        }

        if (e.NewValues["Password"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Password"], "Password can't be null.");
        }
        else
        {
            if (e.NewValues["Password"].ToString().Length < 6)
            {
                AddError(e.Errors, gvwStaffProfile.Columns["Password"], "Password must be at least 6 characters long.");
            }
            else
            {
                if (e.NewValues["Password"].ToString().Length > 32)
                {
                    AddError(e.Errors, gvwStaffProfile.Columns["Password"], "Password must be more than 32 characters long.");
                }
            }
        }

        if (e.NewValues["Firstname"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Firstname"], "First name can't be null.");
        }        

        if (e.NewValues["Lastname"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Lastname"], "Last name can't be null.");
        }
               

        if (e.NewValues["Address"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Address"], "Address can't be null.");
        }

        if (e.NewValues["Phone"] != null)
        {            
            string phone = "^[0-9]{6,12}$";
            rx = new Regex(phone, RegexOptions.IgnoreCase);
            if (e.NewValues["Phone"] != null && !rx.IsMatch(e.NewValues["Phone"].ToString()))
            {
                AddError(e.Errors, gvwStaffProfile.Columns["Phone"], "Phone number invalid. (Ex: 0942076550)");
            }
        }

        if (e.NewValues["Birthdate"] == null)
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Birthdate"], "Birthdate can't be null.");
        }
        else
        {
            DateTime birthdate = Convert.ToDateTime(e.NewValues["Birthdate"].ToString());
            if (birthdate.Year > DateTime.Today.Year)
            {
                AddError(e.Errors, gvwStaffProfile.Columns["Birthdate"], "Birthdate must be at least today");
            }
            else
            {
                if (birthdate.Year == DateTime.Today.Year && birthdate.Month > DateTime.Today.Month)
                {
                    AddError(e.Errors, gvwStaffProfile.Columns["Birthdate"], "Birthdate must be at least today");
                }
                else
                {
                    if (birthdate.Month == DateTime.Today.Month && birthdate.Day >= DateTime.Today.Day)
                    {
                        AddError(e.Errors, gvwStaffProfile.Columns["Birthdate"], "Birthdate must be at least today");
                    }
                }
            }
        }


        string email = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        rx = new Regex(email, RegexOptions.IgnoreCase);
        if (e.NewValues["Email"] != null && !rx.IsMatch(e.NewValues["Email"].ToString()))
        {
            AddError(e.Errors, gvwStaffProfile.Columns["Email"], "Email invalid! Ex:emailname@gmail.com ");
        }
        else
        {
            if (e.HasErrors)
            {
                AddError(e.Errors, gvwStaffProfile.Columns["Email"], "Sorry!");
            }
        }
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        if (errors.ContainsKey(column)) return;
        errors[column] = errorText;
    }    
}